import 'package:fish_redux/fish_redux.dart';
import 'package:kunlun_gas/app_files.dart';
import '../../gas_order/state.dart';
import 'action.dart';
import 'state.dart';
import 'dart:async';

Effect<ChargeReusltViewState> buildEffect() {
  return combineEffects(<Object, Effect<ChargeReusltViewState>>{
    Lifecycle.initState: _initState,
    Lifecycle.dispose: _dispose,
  });
}

void _initState(Action action, Context<ChargeReusltViewState> ctx) async {
  ctx.state.requestTimer =
      Timer.periodic(new Duration(seconds: 2), (timer) async {
    ResultData res = await CommonNetWork.queryOrderStatus(
        ctx.state.orderNo, ctx.state.transNo);
    if (res.success) {
      ctx.state.requestTimer.cancel();
      OrderListItemModel model = OrderListItemModel.fromPayResultJson(res.data);
      Navigator.pop(ctx.context);
      CommonUtils.pushReplaceRoute(ctx.context, RoutePath.orderDetailView,
          arguments: {'model': model});
    } else {
      ctx.state.isFailed = true;
      ctx.dispatch(ChargeReusltViewActionCreator.onAction());
    }
  });
}

void _dispose(Action action, Context<ChargeReusltViewState> ctx) {
  ctx.state.requestTimer.cancel();
}
